package com.baomidou.ant.utils;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 *@description:   身份证工具类
 *@author:        zhoubeibei
 *@email:         2315544970@qq.com
 *@date:          2021/3/25
 *@time:          09:07
 */
public class IdCardUtils {

    //校验方法
    public static boolean isIdCard(String identity) {
        if (identity.length() != 18) {
            return false;
        } else {
            int i, n = 0;
            for (i = 0; i < 17; i++) {
                if (identity.charAt(i) >= '0' && identity.charAt(i) <= '9') {
                    n++;
                } else
                    break;
            }
            if (n <= 16 || !(identity.charAt(17) >= '0' && identity.charAt(17) <= '9' || identity.charAt(17) >= 'a' && identity.charAt(17) <= 'z'))
                return false;
            else {
                int year, month, day;
                year = Integer.valueOf(identity.substring(6, 10));
                month = Integer.valueOf(identity.substring(10, 12));
                day = Integer.valueOf(identity.substring(12, 14));

                String strDate = identity.substring(6, 14);
                // 准备第一个模板，从字符串中提取出日期数字
                String pat1 = "yyyyMMdd";
                // 准备第二个模板，将提取后的日期数字变为指定的格式
                String pat2 = "yyyy-MM-dd";
                SimpleDateFormat sdf1 = new SimpleDateFormat(pat1);        // 实例化模板对象
                SimpleDateFormat sdf2 = new SimpleDateFormat(pat2);        // 实例化模板对象
                Date d = null;
                try {
                    d = sdf1.parse(strDate);   // 将给定的字符串中的日期提取出来
                } catch (Exception e) {            // 如果提供的字符串格式有错误，则进行异常处理
                    e.printStackTrace();       // 打印异常信息
                }
                String r = sdf1.format(d);
                if (r.equals(strDate)) {
                    //校验算法
                    int j, m, sum = 0, mod;
                    int[] k = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
                    for (j = 0; j < 17; j++) {
                        m = Integer.valueOf(identity.substring(j, j + 1));
                        sum += k[j] * m;
                    }
                    mod = sum % 11;
                    String mods = "10x98765432";
                    if (identity.charAt(17) == mods.charAt(mod))
                        return true;
                    else
                        return false;
                } else
                    return false;
            }
        }
    }

    //提取身份证中的年龄
    public static Integer age(String idCard){
        DateTimeFormatter formatterOfYear =DateTimeFormatter.ofPattern("yyyy");
        int nowYear= Integer.parseInt(formatterOfYear.format(LocalDateTime.now()));
        int birthYear=Integer.parseInt(idCard.substring(6,10));
        int age=nowYear-birthYear;
        DateTimeFormatter formatterOfMd =DateTimeFormatter.ofPattern("MMdd");
        int nowMd=Integer.parseInt(formatterOfMd.format(LocalDateTime.now()));
        int birthMd=Integer.parseInt(idCard.substring(10,14));
        if(nowMd>birthMd)
            age=age+1;
        return age;
    }
}